package com.baizhi.controller;


import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.vo.CommonQueryPageVo;
import com.baizhi.vo.CommonVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    UserService userService;

    @RequestMapping("queryAllPage")
    public CommonQueryPageVo queryAllPage(@RequestBody PageDTO pageDTO){
        log.info("当前页：{}",pageDTO.getPage());
        log.info("每页展示数据条数：{}",pageDTO.getPageSize());

        CommonQueryPageVo commonQueryPageVo = userService.queryAllPages(pageDTO.getPage(), pageDTO.getPageSize());

        return commonQueryPageVo;
    }

    /*@RequestMapping("queryAllPage/{page}/{pageSize}")
    public HashMap<String,Object> queryAllPage(@PathVariable("page") Integer pages,@PathVariable("pageSize") Integer pageSize){
        log.info("当前页：{}",pages);
        log.info("每页展示数据条数：{}",pageSize);

        HashMap<String, Object> map = userService.queryAllPage(pages, pageSize);

        return map;
    }*/

    /*@RequestMapping("update")
    public HashMap<String,Object> update(@RequestBody User user){
        log.info("修改用户数据：{}",user);

        HashMap<String, Object> map = userService.update(user);

        return map;
    }*/

    @RequestMapping("update")
    public CommonVo updates(@RequestBody User user){
        log.info("修改用户数据：{}",user);

        CommonVo updates = userService.updates(user);

        return updates;
    }

    @RequestMapping("delete")
    public HashMap<String,Object> delete(@RequestBody User user){
        log.info("删除用户数据：{}",user);
        HashMap<String, Object> map = userService.delete(user);

        return map;
    }

    @RequestMapping("add")
    public CommonVo add(@RequestBody User user){
        log.info("添加用户数据：{}",user);

        try {
            userService.add(user);
            return CommonVo.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVo.faild("添加失败");
        }
    }

    @RequestMapping("uploadHeadImg")
    public HashMap<String,String> uploadHeadImg(MultipartFile headImg){
        log.info("文件名：{}",headImg.getOriginalFilename());
        log.info("文件大小：{}",headImg.getSize());
        log.info("文件类型：{}",headImg.getContentType());


       String s = userService.uploadHeadImgAliyun(headImg);
        log.info("s{}",s);

        HashMap<String, String> map = new HashMap<>();

        map.put("fileName",s);

        return map;
    }

    @RequestMapping("queryById")
    public User queryById(String id){


        return userService.queryById(id);
    }
}
